package com.jf3q.sup.handler;

import com.jf3q.sup.vo.ResultVo;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.jdbc.RuntimeSqlException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler({SQLIntegrityConstraintViolationException.class})
    public ResultVo sqlExp(SQLIntegrityConstraintViolationException e){
       return ResultVo.error(501,"此信息数据与其他数据相连，请先删除与其他关联的数据再进行操作！");
    }

    @ExceptionHandler(RuntimeException.class)
    public ResultVo handleException(Exception e, HttpServletResponse response) {
        if (e instanceof AccessDeniedException){
            response.setStatus(403);
            return null;
        }
        // 在这里处理异常，可以返回自定义的错误信息或进行其他操作
        return ResultVo.error(501,e.getMessage());
    }
}
